package com.dd;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.chantel.tool.db.DBUtil;

import com.dd.exception.*;

public class XiangMuShiYeBu {
	private int xiangMuShiYeId;
	private String ShiYeBuMingCheng;
	
	private String GET_ShiYeInfo= "select * from XiangMuShiYeTable";
	
	public XiangMuShiYeBu(int id,String name)
	{
		this.xiangMuShiYeId = id;
		this.ShiYeBuMingCheng = name;
	}
	
	
	public XiangMuShiYeBu(int id)
	throws XiangMuShiYeNotFoundException{
		this.xiangMuShiYeId = id;
		try
		{
			loadFromDbByID(id);
		}
		catch(XiangMuShiYeNotFoundException em)
		{
			throw em;
		}
		
	}
	
	public XiangMuShiYeBu(String userName)
	throws XiangMuShiYeNotFoundException{
		
		this.ShiYeBuMingCheng  = userName;
		
		try
		{
			loadFromDbByName();
		}
		catch(XiangMuShiYeNotFoundException em)
		{
			throw em;
		}
	}
	private void loadFromDbByID(int id)
	throws XiangMuShiYeNotFoundException{
		// TODO 通过ID加载
		Map whereFields = new HashMap();
		whereFields.put("XiangMuShiYeBuID", new Integer(id));
		DBUtil2 db2 = DBUtil2.getDefaultDBUtil2();
		Map row = db2.getRow(GET_ShiYeInfo, whereFields);
		if(row == null)
		{
			throw new XiangMuShiYeNotFoundException();
		}
		rowDateToFields(row);
	}
	
	//把取到的值传到各个属性上去
	private void rowDateToFields(Map row){
		this.xiangMuShiYeId = ((Integer)row.get("XiangMuShiYeBuID")).intValue();	
		this.ShiYeBuMingCheng = (String)row.get("MingCheng");

	}
	
	private void loadFromDbByName()
	throws XiangMuShiYeNotFoundException{
		// TODO 通过Name加载
		Map whereFields = new HashMap();
		whereFields.put("MingCheng", this.ShiYeBuMingCheng);
		DBUtil2 db2 = DBUtil2.getDefaultDBUtil2();
		Map row = db2.getRow(GET_ShiYeInfo, whereFields);
		
		if(row == null)
		{
			throw new XiangMuShiYeNotFoundException();
		}
		rowDateToFields(row);
		
	}
	
	public String getShiYeBuMingCheng() {
		return ShiYeBuMingCheng;
	}
	public void setShiYeBuMingCheng(String shiYeBuMingCheng) {
		ShiYeBuMingCheng = shiYeBuMingCheng;
	}
	public int getXiangMuShiYeId() {
		return xiangMuShiYeId;
	}
	public void setXiangMuShiYeId(int xiangMuShiYeId) {
		this.xiangMuShiYeId = xiangMuShiYeId;
	}
	
	
}
	